package com.baosight.risk.web.rire;

import com.baosight.risk.api.rire.RireRiskReportService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.enums.IntegralType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.rire.entity.RireRiskTarget;
import com.baosight.risk.service.rire.entity.RireRiskWarningStatus;
import com.baosight.risk.service.rire.vo.RireRiskVO;
import com.baosight.risk.service.rire.vo.RiskWarningVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/rire/import/trport")
@Api(value = "重点风险==披露报告", description = "重点风险==披露报告", produces = MediaType.APPLICATION_JSON)
public class RireRiskReportController extends BaseController {

    @Autowired
    private RireRiskReportService rireRiskReportService;

    /**
     * 披露报告查询列表selctIndex
     *
     * @param warningStatus
     * @return
     */
    @GetMapping("/selectList")
    @Log(title = "披露报告查询列表", integralType = IntegralType.Type2)
    @ApiOperation("披露报告查询列表")
    public AjaxResult saveList(RireRiskWarningStatus warningStatus) {
        if (null == warningStatus) {
            warningStatus = new RireRiskWarningStatus();
        }
        return getResultPage(rireRiskReportService.selectList(warningStatus));
    }

    /**
     * 保存披露报告
     *
     * @param RireRiskVO
     * @return
     */
    @PostMapping("/saveList")
    @Log(title = "保存披露报告")
    @ApiOperation("保存披露报告")
    public AjaxResult saveList(@RequestBody RireRiskVO RireRiskVO) {
        if (null == RireRiskVO) {
            log.error("数据为空！");
            return error("数据为空！");
        }
        log.info("保存披露报告参数：" + RireRiskVO.toString());
        return success(rireRiskReportService.saveRireRisk(RireRiskVO, getUser()));
    }

    /**
     * 披露报告==媛引上年
     *
     * @param circulationId 流转ID
     * @return
     */
    @PostMapping("/saveByLastYear")
    @Log(title = "披露报告==媛引上年")
    @ApiOperation("披露报告==媛引上年")
    public AjaxResult saveByLastYear(String importId, String circulationId, String quarter) {
        if (StringUtils.isEmpty(circulationId) ||
                StringUtils.isEmpty(importId) ||
                StringUtils.isEmpty(quarter)) {
            log.error("数据为空！");
            return error("数据为空！");
        }
        return success(rireRiskReportService.saveByLastYear(importId, circulationId, quarter, getUser()));
    }

    /**
     * 根据ID查询披露报告
     *
     * @param id
     * @return
     */
    @GetMapping("/getReportById")
    @ApiOperation("根据重点风险ID和季度查询披露报告")
    public AjaxResult getReportById(String id) {
        return success(rireRiskReportService.getById(id));
    }

    /**
     * 根据IDS查询披露报告
     *
     * @return
     */
    @PostMapping("/getByIds")
    @ApiOperation("根据IDS查询披露报告")
    public AjaxResult getByIds(@RequestBody List<String> ids) {
        return getResultPage(rireRiskReportService.getByIds(ids));
    }

    /**
     * 根据重点风险ID和指标编码找指标标准值
     *
     * @param importId 重点风险ID
     * @param targetNo 指标编码
     * @param compCode 组织编码
     * @return
     */
    @GetMapping("/selectGoalDetail")
    @ApiOperation("根据重点风险ID和指标编码找指标标准值")
    public AjaxResult selectGoalDetail(String importId, String targetNo, String compCode) {
        if (StringUtils.isEmpty(importId) || StringUtils.isEmpty(targetNo)) {
            log.error("数据为空！");
            return error("数据为空！");
        }
        return success(rireRiskReportService.selectGoalDetail(importId, targetNo, compCode));
    }

    @PostMapping("updateStatus")
    @Log(title = "披露报告状态修改")
    @ApiOperation("披露报告状态修改")
    public AjaxResult updateStatus(@RequestBody MultipleUpdateStatus updateStatus) {
        if (CollectionUtils.isEmpty(updateStatus.getIds())) {
            log.error("参数有误！");
            return error("参数有误！");
        }
        if (StringUtils.isEmpty(updateStatus.getStatus()) || StringUtils.isEmpty(updateStatus.getStatusName())) {
            log.error("参数有误！");
            return error("参数有误！");
        }
        rireRiskReportService.updateStatus(updateStatus);
        return AjaxResult.success();
    }


    /**
     * 风险指标预测下期预测值
     *
     * @param
     * @return
     */
    @PostMapping("/predictNextValue")
    @ApiOperation("风险指标预测下期预测值")
    public AjaxResult predictNextValue(@RequestBody RireRiskTarget target) {
        return success(rireRiskReportService.predictNextValue(target));
    }

    /**
     * 首页用
     *
     * @return
     */
    @GetMapping("/selctIndex")
    @ApiOperation("首页用==披露报告查询列表")
    public AjaxResult selctIndex(String sortFlag) throws Exception {
        return success(rireRiskReportService.selctIndex(sortFlag));
    }


}
